package com.hiwhitley.easy.day03;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by hiwhitley on 2016/5/27.
 */
public class DogCatQueue {
    private Queue<PetEnterQueue> dogQ;
    private Queue<PetEnterQueue> catQ;
    private long count;

    public DogCatQueue() {
        dogQ = new LinkedList<>();
        catQ = new LinkedList<>();
        count = 0;
    }

    public void add(Pet pet) {
        if (pet.getType().equals("Dog")) {
            dogQ.add(new PetEnterQueue(pet, this.count++));
        } else if (pet.getType().equals("Cat")) {
            catQ.add(new PetEnterQueue(pet, this.count++));
        } else {
            throw new RuntimeException("err, not dog or cat");
        }
    }

    private Pet pollAll() {
        if (!this.dogQ.isEmpty() && !this.catQ.isEmpty()) {
            if (this.dogQ.peek().getCount() < this.catQ.peek().getCount()) {
                return dogQ.poll().getPet();
            } else {
                return catQ.poll().getPet();
            }
        }else if (!dogQ.isEmpty()) {
            return dogQ.poll().getPet();
        } else if (!catQ.isEmpty()) {
            return catQ.poll().getPet();
        } else {
            throw new RuntimeException("err, queue is empty");
        }
    }

    public Dog pollDog() {
        if (!isDogQEmpty()) {
            return (Dog) dogQ.poll().getPet();
        } else {
            throw new RuntimeException("Dog queue is empty");
        }
    }

    public Cat pollCat() {
        if (!isCatQEmpty()) {
            return (Cat) catQ.poll().getPet();
        } else {
            throw new RuntimeException("Cat queue is empty");
        }
    }

    public boolean isEmpty() {
        return dogQ.isEmpty() && catQ.isEmpty();
    }

    public boolean isDogQEmpty() {
        return dogQ.isEmpty();
    }

    public boolean isCatQEmpty() {
        return catQ.isEmpty();
    }
}
